package com.osmino.contacts.search;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.SparseIntArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import com.osmino.contacts.entity.Contact;
import com.osmino.contacts.list.ContactItem;

public final class DublicateAdapter extends BaseAdapter implements SectionIndexer {

	private static final int TYPE_CONTACT = 0;
	private static final int TYPE_CONSTRAINT = 1;
	private final Context mContext;
	private final ISearchDublicateController mController;
	private final List<Object> mListItems;
	private final SparseIntArray mTypesArray;
	private final SparseIntArray mSectionForPosition, mPositionForSection;
	private Object[] mSections;
	
	public DublicateAdapter(Context context, ISearchDublicateController controller) {
		mContext = context;
		mController = controller;
		mListItems = new ArrayList<Object>();
		mTypesArray = new SparseIntArray();
		mSectionForPosition = new SparseIntArray();
		mPositionForSection = new SparseIntArray();
		mSections = new Object[0];
		setListSearchStructures(new ArrayList<SearchStructure>());
	}
	
	public void setListSearchStructures(List<SearchStructure> listSearchStructures) {
		mListItems.clear();
		mTypesArray.clear();
		mSectionForPosition.clear();
		mPositionForSection.clear();
		List<Object> sections = new ArrayList<Object>();
		int position = -1;
		int section = -1;
		for (SearchStructure structure : listSearchStructures) {
			position++;
			section++;
			mListItems.add(structure.constraint);
			mTypesArray.put(position, TYPE_CONSTRAINT);
			mSectionForPosition.put(position, section);
			mPositionForSection.put(section, position);
			sections.add(structure.constraint);
			for (Contact contact : structure.contacts) {
				position++;
				mTypesArray.put(position, TYPE_CONTACT);
				mSectionForPosition.put(position, section);
				mListItems.add(contact);				
			}
		}
		mSections = sections.toArray();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mListItems.size();
	}

	@Override
	public Object getItem(int position) {
		return mListItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			view = newView(position, parent);
		}
		bindView(position, view);
		return view;
	}
	
	private View newView(int position, ViewGroup parent) {
		int viewType = getItemViewType(position);
		View item = null;
		if (viewType == TYPE_CONSTRAINT) {
			item = new DublicateConstraintItem(mContext, mController);
		} else if (viewType == TYPE_CONTACT) {
			item = new ContactItem(mContext);
		}
		return item;
	}
	
	private void bindView(int position, View view) {
		int viewType = getItemViewType(position);
		if (viewType == TYPE_CONSTRAINT) {
			DublicateConstraintItem concreteView = (DublicateConstraintItem) view;
			concreteView.setTextAndSection((String) mListItems.get(position), mSectionForPosition.get(position));
		} else if (viewType == TYPE_CONTACT) {
			ContactItem concreteView = (ContactItem) view;
			concreteView.showToggle(true);
			concreteView.setBackground(true);
			concreteView.setInfo((Contact) mListItems.get(position), null);
			concreteView.setAlphabetIndexerOffset(false);
		}
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		return mTypesArray.get(position);
	}

	@Override
	public int getPositionForSection(int sectionIndex) {
		return mPositionForSection.get(sectionIndex);
	}

	@Override
	public int getSectionForPosition(int position) {
		return mSectionForPosition.get(position);
	}

	@Override
	public Object[] getSections() {
		return mSections;
	}
	
	public void setNewConstraintForSection(int section, String constraint) {
		int position = mPositionForSection.get(section);
		mListItems.set(position, constraint);
	}

}
